import Vue from 'vue'
import Router from 'vue-router'
// i18n    t locale
// Vue.use $t $locale
import i18n from '@/assets/lang'
const title = i18n.t('title')  // document.title
// vue-router 3.0+版本 $router.push 方法改为promise
// NavigationDumplicated
// push方法统一进行catch异常处理 会报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  // catch 必须处理
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

import layout from '@/views/layout'

export const constantRoutes = [
  {
    path: '/',
    component: layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: () => import(/* webpackChunkName: "home" */'@/views/home/index.vue'),
        meta: { title: title.home, /** title roles here */ }
      }
    ]
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: 'network',
        component: () => import('@/views/network'),
        redirect: '/network/wan',
        children: [
          { path: 'wan', component: ()=> import('@/views/network/wan'), meta: {title: 'WAN'}},
          { path: 'net_type', component: ()=> import('@/views/network/net_type'), meta: {title: '3G/4G'}},
          { path: 'lan', component: ()=> import('@/views/network/lan'), meta: {title: 'LAN'}},
          { path: 'dhcp', component: ()=> import('@/views/network/dhcp'), meta: {title: 'DHCP'}},
          { path: 'router', component: ()=> import('@/views/network/router'), meta: {title: i18n.t('title.router')}},
          { path: 'terminal', component: ()=> import('@/views/network/terminal'), meta: {title: i18n.t('title.terminal')}},
          { path: 'qos', component: ()=> import('@/views/network/qos'), meta: {title: i18n.t('title.qos')}},
          { path: 'nat', component: ()=> import('@/views/network/nat'), meta: {title: i18n.t('title.nat')}},
        ]
      }
    ]
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: 'wifi',
        component: () => import('@/views/wifi'),
        redirect: '/wifi/2.4g',
        children: [
          { path: '2.4g', component: ()=>import('@/views/wifi/2.4g'), meta: {title: '2.4G'} },
          { path: '5g', component: ()=>import('@/views/wifi/5g'), meta: {title: '5G'} },
          { path: 'visitor', component: ()=>import('@/views/wifi/visitor'), meta: {title: i18n.t('title.visitor')} },
          { path: 'signal', component: ()=>import('@/views/wifi/signal'), meta: {title: i18n.t('title.signal')} },
          { path: 'scan', component: ()=> import('@/views/wifi/scan'), meta: {title: i18n.t('title.scan')} }, 
        ]
      }
    ]
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/advanced',
        component: resolve => require(['@/views/advanced'], resolve),
        meta: { title: '高级设置'},
        // 控制缓存的路由是否刷新
        beforeEnter(to, from, next){
          // console.log(from.path)
          if(from.path == '/'){
            // 组件还未创建 无法访问this 先保存在路由元字段
            to.meta.refresh = true
          }
          next()
        },
        redirect: '/advanced/security',
        children: [
          { path: 'security', component: ()=> import('@/views/advanced/security')},
          { path: 'dmz_host', component: ()=> import('@/views/advanced/dmz_host')},
          { path: 'ddns', component: ()=> import('@/views/advanced/ddns')},
          { path: 'dispatch', component: ()=> import('@/views/advanced/dispatch')},
          // { path: 'nameList', component: ()=> import('@/views/advanced/nameList')},
          { path: "netwatchdog", component: ()=> import("@/views/advanced/netwatchdog")},
        ]
      }
    ]
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/service',
        component: () => import('@/views/service'),
        redirect: '/service/cloud_login',
        children: [
          // { path: 'cloud', component: ()=> import('@/views/service/cloud')},
          { path: 'cloud_login', component: ()=> import('@/views/service/cloud_login'), meta: {title: i18n.t('title.cloud_login')}},
          { path: 'cloud_control', component: ()=> import('@/views/service/cloud_control'), meta: {title: i18n.t('title.cloud_control')}},
          { path: 'vpn', component: () => import('@/views/service/vpn.vue')},
          { path: 'certification', component: ()=> import('@/views/service/certification')},
          { path: 'disk', component: ()=> import('@/views/service/disk')},
          { path: 'file', component: ()=> import('@/views/service/file')},
          { path: 'pptp', component: ()=> import('@/views/service/pptp')},
          { path: 'l2tp', component: ()=> import('@/views/service/l2tp')},
          { path: 'openvpn', component: ()=> import('@/views/service/openvpn')},
          { path: 'mltcp', component: ()=> import('@/views/service/mltcp')},
          { path: 'tunnel', component: ()=> import('@/views/service/tunnel')},
        ]
      }
    ]
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/system',
        component: () => import('@/views/system'),
        redirect: '/system/update',
        children: [
          { path: 'update', component: ()=> import('@/views/system/update'), meta: { title: title.sys_upgrade}},
          { path: 'backup', component: () => import('@/views/system/backup'),  meta: { title: title.sys_backup}},
          { path: 'settings', component: ()=> import('@/views/system/settings')},
        ]
      }
    ]
  },
  {
    path: '/',
    component: layout,
    children: [
      {
        path: '/utils',
        component: () => import('@/views/utils'),
        redirect: '/utils/diagnostics',
        children: [
          { path: 'diagnostics', component: ()=> import('@/views/utils/diagnostics'), meta: {title: i18n.t('title.diagnostics')}},
          { path: 'investigate', component: () => import('@/views/utils/investigate'), meta: {title: i18n.t('title.investigate')}},
        ]
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    name: 'login',
    meta: {
      title: i18n.t('title.login'),
    }
  },
  {
    path: '*',
    name: '404',
    component: () => import(/* webpackChunkName: "404" */ '@/views/404.vue')
  }
]

const createRouter = () => new Router({
    // base: '/os',
    mode: 'hash',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()

function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export { resetRouter, }

export default router
